library(plyr)
library(tidyverse)
library(magrittr)
library(foreign)
library(estimatr)
library(emmeans)
library(broom)
library(countrycode)

"https://github.com/thomasjwood/kz_multicountry/raw/main/t1.rds" %>% 
  url %>% 
  gzcon %>% 
  readRDS %>% 
  filter(
    condition == "Control - only outcome"
  ) %>% 
  mutate(
    outcome = outcome %>% 
      str_detect("Not ") %>% 
      ifelse(
        "0",
        "1"
      ) %>% 
      as.numeric
  ) %>% 
  group_by(
    geo_country_code, issue
  ) %>% 
  summarize(
    mu = outcome %>% 
      weighted.mean(weight, na.rm  =T),
    se = outcome %>% 
      diagis::weighted_se(weight, na.rm = T)
  ) %>%
  mutate(
    lo = mu %>% subtract(se %>% multiply_by(1.96)),
    hi = mu %>% add(se %>% multiply_by(1.96)),
    nlab = mu %>% round(2)
  ) %>% 
  modify_at(3:7, ~multiply_by(., 100)) %>% 
  ungroup %>% 
  mutate(
    issue2 = issue %>% 
      equals("country") %>% 
      not %>% 
      ifelse(
        mapvalues(
          issue %>% as.character, 
          c("global 1",
            "global 2"),
          c("The Gates Foundation is going to make huge profits from COVID-19 vaccines.",
            "mRNA vaccines like Moderna permanently alter DNA, according to Moderna executives. ")
        ),
        mapvalues(
          geo_country_code %>% 
            as.character, 
          c("BR", "DE", "FR", "ID", "IN",
            
            "MX", "NG", "PE", "US", "ZA"),
          c("Argentine army to enforce Covid quarantine",
            "Government will secretly vaccinate infants",
            "Vaccinated people more contagious than unvaccinated",
            "Covid vaccine causes fatal lymphocytosis",
            "Covid vaccine causes erectile dysfunction",
            
            "Tea more effective than vaccines in preventing Covid",
            "Government bought low quality vaccines",
            "Novel Covid variants are in the Covid vaccine",
            "Covid vaccine affects menstrual cycles",
            "mRNA vaccines will alter genetic material")
        )
      ),
    il2 = issue %>% 
      equals("country") %>% 
      ifelse(
        geo_country_code %>% 
          countrycode("iso2c", "country.name") %>% 
          str_c(
            "-",
            issue2 %>% as.character
          ),
        geo_country_code %>% 
          countrycode("iso2c", "country.name")
      ) %>% 
      str_c(
        " (",
        issue %>% 
          factor %>% 
          as.numeric,
        ")"
      )
  ) %>% 
  arrange(desc(mu)) %>% 
  mutate(
    issue3 = issue %>% 
      equals("country") %>% 
      ifelse(
        "Country specific",
        issue2
        ),
    il2 = il2  %>%
      fct_reorder(mu),
    issue3 = issue3 %>% 
      fct_reorder(mu, .desc = T)
    ) %>% 
  ggplot() +
  geom_linerange(
    aes(
      xmin = lo, xmax = hi, y = il2
    )
  ) +
  geom_point(
    aes(
      mu, il2
    ),
    shape = 21,
    fill = "white",
    size = 5.5
  ) +
  geom_text(
    aes(
      mu, il2, label = nlab
    ),
    size = 2
    ) +
  facet_grid(
    issue3 ~ ., 
    scales = "free_y", 
    labeller = label_wrap_gen(width = 15)
  ) +
  # scale_y_discrete(
  #   breaks = t4$il2 %>% 
  #     levels,
  #   labels = t4$il2 %>% 
  #     levels %>% 
  #     str_sub(, -5)
  # ) +
  labs(
    x = "Agree or Strongly Agree (%)",
    y = ""
  )